package com.aiju.cash.ui.billing.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.fragment.BaseFragment;
import com.aiju.bean.Good;
import com.aiju.cash.R;
import com.aiju.cash.ui.good.manager.GoodManager;
import com.aiju.cash.ui.pay.util.AdditionMoney;
import com.aiju.utils.T;

/**
 * @author: 佐为
 * @date: 2016-12-14 09:39
 * @version: V1.0
 * @Description: 现金支付
 */
public class PaymentFragment extends BaseFragment implements View.OnClickListener {

    private int GoodNameNumber = 1;
    private String GoodName = "输入金额";
    private String GoodId = "10000000";
    private String GoodTypeId = "10000000";

    private AdditionMoney mAdditionMoney;

    private TextView mMoneyTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_payment, container, false);
        init(root);
        return root;
    }

    private void init(final View root) {
        mMoneyTextView = (TextView) root.findViewById(R.id.tv_billing_money);
        root.findViewById(R.id.tv_pay_1).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_2).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_3).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_4).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_5).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_6).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_7).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_8).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_9).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_0).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_point).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_c).setOnClickListener(this);
        root.findViewById(R.id.iv_pay_back).setOnClickListener(this);
        root.findViewById(R.id.iv_pay_add).setOnClickListener(this);
        root.findViewById(R.id.tv_pay_pay).setOnClickListener(this);

        ((TextView) root.findViewById(R.id.tv_pay_pay)).setText(getString(R.string.add_order));
        ImageView c= (ImageView) root.findViewById(R.id.iv_pay_add);
        TextView oo= (TextView) root.findViewById(R.id.tv_pay_c);
        c.setImageResource(R.mipmap.icon_pay_c);
        oo.setText("00");

        mAdditionMoney = new AdditionMoney();
        mAdditionMoney.setMoneyChange(new AdditionMoney.OnMoneyChange() {
            @Override
            public void onMoneyChange(String favorable, String printAllMoney, String totalMoney, String receivableMoney) {
                if (TextUtils.isEmpty(printAllMoney)) {
                    root.findViewById(R.id.tv_billing_tip).setVisibility(View.VISIBLE);
                    mMoneyTextView.setVisibility(View.GONE);
                } else {
                    root.findViewById(R.id.tv_billing_tip).setVisibility(View.GONE);
                    mMoneyTextView.setVisibility(View.VISIBLE);
                    if (printAllMoney.contains("+")) {
                        mMoneyTextView.setText(totalMoney);
                    } else {
                        mMoneyTextView.setText(printAllMoney);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_pay_0:
                mAdditionMoney.onKeyDown(0);
                break;
            case R.id.tv_pay_1:
                mAdditionMoney.onKeyDown(1);
                break;
            case R.id.tv_pay_2:
                mAdditionMoney.onKeyDown(2);
                break;
            case R.id.tv_pay_3:
                mAdditionMoney.onKeyDown(3);
                break;
            case R.id.tv_pay_4:
                mAdditionMoney.onKeyDown(4);
                break;
            case R.id.tv_pay_5:
                mAdditionMoney.onKeyDown(5);
                break;
            case R.id.tv_pay_6:
                mAdditionMoney.onKeyDown(6);
                break;
            case R.id.tv_pay_7:
                mAdditionMoney.onKeyDown(7);
                break;
            case R.id.tv_pay_8:
                mAdditionMoney.onKeyDown(8);
                break;
            case R.id.tv_pay_9:
                mAdditionMoney.onKeyDown(9);
                break;
            case R.id.tv_pay_point:
                mAdditionMoney.onKeyDown(AdditionMoney.POINT);
                break;
            case R.id.tv_pay_c://改变为00
                mAdditionMoney.onKeyDown(AdditionMoney.ZERO);
                break;
            case R.id.tv_pay_pay:
                pay();
                break;
            case R.id.iv_pay_back:
                mAdditionMoney.onKeyDown(AdditionMoney.BACK);
                break;
            case R.id.iv_pay_add: //改变为c
                mAdditionMoney.onKeyDown(AdditionMoney.CLEAR);
                break;
        }
    }

    /**
     * 构建默认现金商品
     */
    private void pay() {
        if (!mAdditionMoney.getTotalMoney().equals("0.00")) {
            Good good = new Good();
            good.setName(GoodName + GoodNameNumber);
            good.setSelectSum(1);
            good.setId(GoodId + GoodNameNumber);
            good.setGoodsTypeId(GoodTypeId);
            good.setPrice(mAdditionMoney.getTotalMoney());
            GoodNameNumber++;
            GoodManager.getInstance(getContext()).add(good, true);
            T.ToastShort("已加入购物车");
            mMoneyTextView.setText("0.00");
            mAdditionMoney.onKeyDown(AdditionMoney.CLEAR);
        } else {
            T.ToastShort("请输入商品价格");
        }
    }

}
